//
//  ArtistTableVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/23.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "ArtistTableVC.h"
#import "ArtistCell_first.h"
#import "ArtistCell_second.h"
#import "IntroViewController.h"
#import "IGArtistApi.h"
#import "IGArtGalleryAttentApi.h"

@interface ArtistTableVC ()
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, copy) NSString *pageSize;
@property (nonatomic, strong) IGArtistApi *api;
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;
@property (nonatomic, strong) NSMutableArray *memberList;
@property (nonatomic, strong) NSMutableArray *artistList;

@end

@implementation ArtistTableVC

- (void)viewDidLoad
{
    [super viewDidLoad];
//    self.title = @"艺术家";
    self.title = NSLocalizedString(@"Artists", nil);
    self.tableView.backgroundColor = [UIColor c2Color];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ArtistCell_first class] forCellReuseIdentifier:@"ArtistCell_first"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ArtistCell_second" bundle:nil] forCellReuseIdentifier:@"ArtistCell_second"];
    [self.tableView ig_setHeaderWithRefreshingBlock:^{
        _pageSize = @"10";
        _pageNum = @"1";
        [self dataRequest];
    }];
    self.api = [IGArtistApi new];
    self.attentApi = [IGArtGalleryAttentApi new];

    _pageSize = @"10";
    _pageNum = @"1";
    self.memberList = [@[] mutableCopy];
    self.artistList = [@[] mutableCopy];
    [self dataRequest];
}

- (void)dataRequest
{
    self.api.pageSize = _pageSize;
    self.api.pageNum = _pageNum;
    @KWeakObj(self)
    [self.api startWithDecodeClass:[IGArtistResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.tableView ig_endHeaderRefreshing];
        [self.tableView ig_endFooterRefreshing];
        if (responseObject)
        {
            IGArtistResp *resp = responseObject;
            IGArtistModel *model = resp.data;
            if (model.memberList && model.memberList.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.tableView ig_endFooterRefreshingWithNoMoreData];
            }
            _artistList = model.artistList;
            if ([_pageNum isEqualToString:@"1"])
            {
                _memberList = [NSMutableArray arrayWithArray:model.memberList];
            }
            else
            {
                [_memberList addObjectsFromArray:model.memberList];
            }
            
            [selfWeak.tableView reloadData];
        }
        else
        {
            
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.tableView ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak dataRequest];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger i = _memberList.count ? (_memberList.count + (_artistList.count?1:0)) : (_artistList.count?1:0);
    return i;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section != 0)
    {
        IntroViewController *introVC = [[IntroViewController alloc] init];
        introVC.u_id =  _memberList[indexPath.section - 1][@"id"];
        [UIViewController ig_pushViewController:introVC animated:YES];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        ArtistCell_first *cell = [tableView dequeueReusableCellWithIdentifier:@"ArtistCell_first" forIndexPath:indexPath];
        if (_artistList.count)
        {
            cell.dataArr = [NSMutableArray arrayWithArray:_artistList];
        }
        return cell;
    }
    else
    {
        @KWeakObj(self)
        ArtistCell_second *cell = [tableView dequeueReusableCellWithIdentifier:@"ArtistCell_second" forIndexPath:indexPath];
        if (_memberList.count)
        {
            @KWeakObj(cell)
            cell.dic = _memberList[indexPath.section - 1];
            cell.attentionBtnBlock = ^(UIButton *btn) {
                [selfWeak attentDataRequest:[NSString stringWithFormat:@"%@", btn.selected ? @"1" : @"0"] dic:[NSMutableDictionary dictionaryWithDictionary:cellWeak.dic] section:indexPath.section];
            };
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }    
    return nil;
}

- (void)attentDataRequest:(NSString *)operatorType dic:(NSMutableDictionary *)dic section:(NSUInteger)section
{
    self.attentApi.operatorType = operatorType;
    self.attentApi.museumId = dic[@"id"];
    @KWeakObj(self)
    [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            NSString *str = [operatorType isEqualToString:@"0"] ? @"0" : @"1";
            [dic setValue:str forKey:@"attentionState"];
            [_memberList replaceObjectAtIndex:section - 1 withObject:dic];
        
            [UIView performWithoutAnimation:^{
                [selfWeak.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
            }];
            
//            [selfWeak.tableView reloadData];
            if ([operatorType isEqualToString:@"1"])
            {
                
//                [selfWeak presentMessageTips:@"取消成功"];
//                [selfWeak presentMessageTips:NSLocalizedString(@"Canceled Successfully", nil)];
            }
            else
            {
//                [selfWeak presentMessageTips:@"关注成功"];
//                [selfWeak presentMessageTips:NSLocalizedString(@"Follow Successfully", nil)];
            }
        }
    }];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 100;
    }
    return (KSCREENWIDTH - 54) / 3 + 90;
}

@end
